package halk;

import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class ESClientDemo {

    private RestClient restClient;

    @Before
    public void before() {
        System.out.println("method before...");
        restClient = RestClient.builder(new HttpHost("127.0.0.1", 9200)).build();
    }

    @Test
    public void connect() {
        System.out.println(restClient);
        List<Node> nodes = restClient.getNodes();
        for (Node node : nodes) {
            System.out.println(node);
        }
    }

    @Test
    public void get() throws IOException {
        Response response = restClient.performRequest(new Request("get", "/_search"));
        System.out.println(response.getRequestLine());
        System.out.println(EntityUtils.toString(response.getEntity()));
        System.out.println(response.getStatusLine());

//        Request request = new Request("get", "/_search");
//        restClient.performRequestAsync(request,
//                 new ResponseListener() {
//                     @Override
//                     public void onSuccess(Response response) {
//                         System.out.println("success:" + response.toString());
//                     }
//
//                     @Override
//                     public void onFailure(Exception e) {
//                         System.out.println("failed:" + e);
//                         e.printStackTrace();
//                     }
//                 });

    }
}
